/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年4月9日         Root.Lu
 */

package com.coocaa.x.service.litecontent.providers.system.host;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.service.litecontent.LiteContentProvider;
import com.coocaa.x.service.litecontent.LiteContentProvider.LiteContentProviderHost;
import com.coocaa.x.service.litecontent.LiteContentProvider.ResourceHandler;
import com.coocaa.x.service.litecontent.data.ResultPackage;
import com.skyworth.framework.skysdk.ipc.SkyCmdURI;
import com.skyworth.framework.skysdk.ipc.SkyCmdURI.SkyCmdPathErrorException;

import java.net.URISyntaxException;

public class SystemHost_Com_Coocaa_CCAX extends LiteContentProviderHost {
    private static SystemHost_Com_Coocaa_CCAX instance = null;

    public static synchronized void create(Context context, LiteContentProvider provider) {
        if (instance == null)
            instance = new SystemHost_Com_Coocaa_CCAX(context, provider);
    }

    private static final String MY_HOST = "com.coocaa.ccAx";

    private static SkyCmdURI getUserUri(String cmd) {
        SkyCmdURI uri = null;
        try {
            uri = new SkyCmdURI("tianci://" + SELF + "?cmd=" + cmd);
        } catch (URISyntaxException e) {
            LogUtils.e(MY_HOST, "URISyntaxException = " + e.getMessage());
        } catch (SkyCmdPathErrorException e) {
            LogUtils.e(MY_HOST, "SkyCmdPathErrorException = " + e.getMessage());
        }
        return uri;
    }

    private static final String SELF = "com.coocaa.aseriesfloatview" + "/"
            + "com.coocaa.aseriesfloatview.MainpageService";

    public SystemHost_Com_Coocaa_CCAX(Context context, LiteContentProvider provider) {
        super(context, provider);
    }

    @Override
    public String myHost() {
        return MY_HOST;
    }

    @SuppressWarnings("unused")
    // 2.3.1、信号源
    private ResourceHandler tv_source_list = new ResourceHandler(this, "/tv_source_list") {
        @Override
        public ResultPackage call(Uri uri) {
            ResultPackage rp = new ResultPackage();
            SkyCmdURI _uri = getUserUri("ShowSourcePage");
            if (_uri != null) {
                try {
                    XServiceManager.getLiteCCConnectorService().sendCmd(_uri.getCmdUrl(),
                            new byte[0]);
                } catch (Exception e) {
                    rp.error_code = -1;
                    rp.error_msg = e.getMessage();
                }
            } else
                rp.error_code = -1;
            return rp;
        }
    };

    @SuppressWarnings("unused")
    // 2.3.2、設置
    private ResourceHandler system_setting = new ResourceHandler(this, "/system_setting") {
        @Override
        public ResultPackage call(Uri uri) {
            ResultPackage rp = new ResultPackage();
            SkyCmdURI _uri = getUserUri("ShowSetting");
            if (_uri != null) {
                try {
                    XServiceManager.getLiteCCConnectorService().sendCmd(_uri.getCmdUrl(),
                            new byte[0]);
                } catch (Exception e) {
                    rp.error_code = -1;
                    rp.error_msg = e.getMessage();
                }
            } else
                rp.error_code = -1;
            return rp;
        }
    };

    @SuppressWarnings("unused")
    // 2.3.3、本地媒體
    private ResourceHandler local_media = new ResourceHandler(this, "/local_media") {
        @Override
        public ResultPackage call(Uri uri) {
            ResultPackage rp = new ResultPackage();
            try {
                Intent arg0 = new Intent();
                arg0.setClassName("com.tianci.localmedia", "com.tianci.localmedia.SkyLocalMedia");
                arg0.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(arg0);
            } catch (Exception e) {
                rp.error_code = -1;
                rp.error_msg = e.getMessage();
            }
            return rp;
        }
    };
}
